/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.common.logger;

/**
 * Console can be used to print trace, info, warning and error messages
 * 
 * @author Laurent GUERIN
 *  
 */

import java.io.PrintStream;

/* package */ final class Console
{
    private static int STACK_MAX_CALLERS = 10 ;
    
    public static final PrintStream out = System.out;

    //-------------------------------------------------------------------------------------
    /**
     * Print a trace message
     * @param sClassName
     * @param sMsg
     */
    public static void trace(String sClassName, String sMsg)
    {
        println("Trace : [" + sClassName + "] " + sMsg);
    }

    /**
     * Print a trace message
     * @param sMsg
     */
    public static void trace(String sMsg)
    {
        println("Trace : " + sMsg);
    }

    //-------------------------------------------------------------------------------------
    /**
     * Print an error message
     * @param sClassName
     * @param sMsg
     */
    public static void error(String sClassName, String sMsg)
    {
        println("ERROR : [" + sClassName + "] " + sMsg);
    }

    /**
     * Print an error message
     * @param sMsg
     */
    public static void error(String sMsg)
    {
        println("ERROR : " + sMsg);
    }

    //-------------------------------------------------------------------------------------
    /**
     * Print a warning message
     * @param sClassName
     * @param sMsg
     */
    public static void warn(String sClassName, String sMsg)
    {
        println("WARN  : [" + sClassName + "] " +  sMsg);
    }

    /**
     * Print a warning message
     * @param sMsg
     */
    public static void warn(String sMsg)
    {
        println("WARN  : " + sMsg);
    }

    //-------------------------------------------------------------------------------------
    /**
     * Print an info message
     * @param sClassName
     * @param sMsg
     */
    public static void info(String sClassName, String sMsg)
    {
        println("info  : [" + sClassName + "] " + sMsg);
    }

    /**
     * Print an info message
     * @param sMsg
     */
    public static void info(String sMsg)
    {
        println("info  : " + sMsg);
    }

    //-------------------------------------------------------------------------------------
    /**
     * Reports an exception on the console
     * @param ex
     */
    public static void exeptionReport( Throwable ex )
    {
        exeptionReport( ex, false );
    }
    //-------------------------------------------------------------------------------------
    /**
     * Reports an exception on the console
     * @param ex
     * @param bCause
     */
    public static void exeptionReport( Throwable ex, boolean bCause )
    {
        String sCause = bCause ? "CAUSE " : "" ;
        println("EXCEPTION " + sCause + ": [" + ex.getClass().getName() + "] : \"" + ex.getMessage() + "\"" );
        StackTraceElement st[] = ex.getStackTrace();
        StackTraceElement loc = null;
        if ( st.length > 0 )
        {
            //--- Exception origin ( 0 ) 
	        loc = st[0];
            //println(" in '" + loc.getFileName() + "', line " + loc.getLineNumber() + ", method '" + loc.getMethodName() + "'" );
            //println(" in '" + loc.getClassName() + "', line " + loc.getLineNumber() + ", method '" + loc.getMethodName() + "', callers :" );
            //--- Callers stack ( 1 to N )
	        for ( int i = 0 ; i < st.length && i < STACK_MAX_CALLERS+1 ; i++ )
	        {
	            loc = st[i];
	            println(" . " + loc.getClassName() + "." + loc.getMethodName() + " : " + loc.getLineNumber() );
	        }
        }
        if ( ex.getCause() != null )
        {
            exeptionReport( ex.getCause(), true );
        }
    }
    

    //-------------------------------------------------------------------------------------
    /**
     * Print a String
     * @param s
     */
    synchronized static private void println(String s)
    {
        System.out.println(s);
        System.out.flush();
    }
}